package net.zoneland.zrdp.common.core.page;

import org.apache.commons.lang3.StringUtils;

import net.zoneland.zrdp.common.utils.StringUtilPlus;

import java.util.Objects;
import java.util.Optional;

/**
 * 分页数据
 *
 * @author zonevue
 */
public class PageDomain
{
    /** 当前记录起始索引 */
    private Integer pageNum;

    /** 每页显示记录数 */
    private Integer pageSize;

    /** 排序列 */
    private String orderByColumn;

    /** 排序的方向desc或者asc */
    private String isAsc = "asc";

    /** 分页参数合理化 */
    private Boolean reasonable = Boolean.TRUE;

    public String getOrderBy() {
        return Optional.ofNullable(this.orderByColumn).filter(StringUtils::isNotBlank).map(cloumn -> {

            //处理后的 列名  例如： user_id
            final String columnStr = StringUtilPlus.toUnderScoreCase(cloumn);
            // 加上 is null 目的是把字段为null 记录都放在最后。
            return String.format("%s IS NULL, %s %s", columnStr, columnStr, this.isAsc);
        }).orElse(StringUtils.EMPTY);
    }

    public Integer getPageNum()
    {
        return pageNum;
    }

    public void setPageNum(Integer pageNum)
    {
        this.pageNum = pageNum;
    }

    public Integer getPageSize()
    {
        return pageSize;
    }

    public void setPageSize(Integer pageSize)
    {
        this.pageSize = pageSize;
    }

    public String getOrderByColumn()
    {
        return orderByColumn;
    }

    public void setOrderByColumn(String orderByColumn)
    {
        this.orderByColumn = orderByColumn;
    }

    public String getIsAsc()
    {
        return isAsc;
    }

    public void setIsAsc(String isAsc)
    {
        if (StringUtils.isNotEmpty(isAsc))
        {
            // 兼容前端排序类型
            if ("ascending".equals(isAsc))
            {
                isAsc = "asc";
            }
            else if ("descending".equals(isAsc))
            {
                isAsc = "desc";
            }
            this.isAsc = isAsc;
        }
    }

    public Boolean getReasonable()
    {
        if (Objects.isNull(reasonable))
        {
            return Boolean.TRUE;
        }
        return reasonable;
    }

    public void setReasonable(Boolean reasonable)
    {
        this.reasonable = reasonable;
    }
}
